package main;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Iterator;

import problem.Heuristic;
import problem.logic;
import utils.AstarStrategy;
import utils.GreedyStrategy;
import utils.ShowGraphGui;
import agents.Agent;
import agents.Greedy;
import agents.Human;
import agents.Search;
import agents.TerroristBuster;
import env.Move;
import env.World;

public class Main {
	// constants
	public static final int noOpCost = 1, hellBreakLooseCost = 10000,
			cutoff = 50,f=10000;

	/**
	 * @param args
	 */
	public static void main2(String[] args) {

		World world = utils.Parser.parseWorldFile("world.txt");
		World world2 = new World(world);
		System.out.print(world2.getCities().equals(world.getCities()));
	}

	public static void main(String[] args) {

		World world = utils.Parser.parseWorldFile("world.txt");
		int cost = 0;
		World copiedWorld = null;
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String line;
		System.out.print("how many agents do you wish to make?:");
		try {
			line = br.readLine();
			int numRead = Integer.parseInt(line);
			for (int i = 0; i < numRead; i++) {
				System.out
						.println("type of agent\n\t1:human\n\t2:greedy\n\t3:terrorist buster\n\t4:greedy search\n\t5:A*search\n\t6:real time A* search");
				line = br.readLine();
				int agentType = Integer.parseInt(line);
				System.out.println("type initial city of agent");
				line = br.readLine();
				String initCity = new String(line);
				System.out.println("type goal city of agent");
				line = br.readLine();
				String goalCity = new String(line);
				Agent nAgent = null;

				switch (agentType) {
				case 1:
					nAgent = new Human(world, world.getCityByName(initCity),
							world.getCityByName(goalCity));
					world.addAgent(nAgent, world.getCityByName(initCity));
					break;
				case 2:
					nAgent = new Greedy(world, world.getCityByName(initCity),
							world.getCityByName(goalCity));
					world.addAgent(nAgent, world.getCityByName(initCity));
					break;
				case 3:
					nAgent = new TerroristBuster(world,
							world.getCityByName(initCity),
							world.getCityByName(goalCity));
					world.addAgent(nAgent, world.getCityByName(initCity));
					break;
				case 4:
					copiedWorld = new World(world);
					nAgent = new Search(world, copiedWorld,
							world.getCityByName(initCity),
							world.getCityByName(goalCity), new GreedyStrategy(
									copiedWorld.getCityByName(goalCity)), 0);
					world.addAgent(nAgent, world.getCityByName(initCity));
					copiedWorld.addAgent(nAgent,copiedWorld.getCityByName(initCity));
					break;
				case 5:
					copiedWorld = new World(world);
					nAgent = new Search(world, copiedWorld,
							world.getCityByName(initCity),
							world.getCityByName(goalCity), new AstarStrategy(
									copiedWorld.getCityByName(goalCity)), 0);
					world.addAgent(nAgent, world.getCityByName(initCity));
					copiedWorld.addAgent(nAgent,copiedWorld.getCityByName(initCity));
					break;
				case 6:
					copiedWorld = new World(world);
					nAgent = new Search(world, copiedWorld,
							world.getCityByName(initCity),
							world.getCityByName(goalCity), new AstarStrategy(
									copiedWorld.getCityByName(goalCity)),
							cutoff);
					world.addAgent(nAgent, world.getCityByName(initCity));
					copiedWorld.addAgent(nAgent,copiedWorld.getCityByName(initCity));
					break;
				}
			}
			int counter = 0;
			while (!world.allDone()) {
				Iterator<Agent> it = world.getAgents().iterator();

				while (it.hasNext()) {
					Agent agent = it.next();
					if (!agent.isDone()) {
						Move move = agent.calcMove();
						int calcCost = logic.calcCost(move);
						if (move != null) {
							System.out.println(move
									+ " move cost: "
									+ calcCost
									+ " total cost: "
									+ (cost + calcCost)
									+ " heuristic: "
									+ Heuristic.getHeuristic(world, world
											.getCityByName(move.agent.getGoal()
													.getName())));
							world.moveAgents(move);
							counter++;
						}
						cost += calcCost;
					}
				}
			}
			Iterator<Agent> it = world.getAgents().iterator();
			int expandCost = 0;
			while (it.hasNext())
				expandCost += it.next().getExpands();
			System.out.println("All done, move cost:" + cost
					+ " expand amount:" + expandCost + " P="
					+ (cost*f + expandCost));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
